import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class PaperListTest {
    private PaperList paperList;

    @Before
    public void setUp() {
        List<Paper> papers = Arrays.asList(
                new Thesis("Thesis 1"),
                new JournalPaper("Journal 1"),
                new ConferencePaper("Conference 1"),
                new Thesis("Thesis 2")
        );
        paperList = new PaperList("Test Teacher", papers);
    }

    @Test
    public void testSelectPaperByRandomSuccess() {
        Student student = new Student("Alice", 1, PaperType.Thesis);
        try {
            List<Paper> selected = paperList.selectPaperByRandom(student);
            assertNotNull(selected);
            assertEquals(1, selected.size());
            assertTrue(selected.get(0) instanceof Thesis);
            assertTrue(paperList.getSelectionResult().isSelected(selected.get(0)));
        } catch (NoEnoughPapersException e) {
            fail("No exception should be thrown");
        }
    }

    @Test
    public void testSelectPaperByRandomNoEnough() {
        Student student = new Student("Bob", 5, PaperType.Any);
        try {
            paperList.selectPaperByRandom(student);
            fail("Should throw NoEnoughPapersException");
        } catch (NoEnoughPapersException e) {
            assertEquals("学生 Bob 希望选论文 5 篇，但目前只有 4 篇可选", e.getMessage());
        }
    }

    @Test
    public void testSelectPaperByRandomWithType() {
        Student student = new Student("Charlie", 1, PaperType.JournalPaper);
        try {
            List<Paper> selected = paperList.selectPaperByRandom(student);
            assertNotNull(selected);
            assertEquals(1, selected.size());
            assertTrue(selected.get(0) instanceof JournalPaper);
        } catch (NoEnoughPapersException e) {
            fail("No exception should be thrown");
        }
    }

    @Test
    public void testSetSelectionStrategy() {
        Student student = new Student("David", 2, PaperType.Any);
        paperList.setSelectionStrategy(new OrderSelectionStrategy());
        try {
            List<Paper> selected = paperList.selectPaperByRandom(student);
            assertNotNull(selected);
            assertEquals(2, selected.size());
            // 顺序选择策略应返回前两篇论文
            assertEquals("Thesis 1", selected.get(0).getTitle());
            assertEquals("Journal 1", selected.get(1).getTitle());
        } catch (NoEnoughPapersException e) {
            fail("No exception should be thrown");
        }
    }
}    